﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ThoiTrangT2B.Admin
{
    public partial class QuanTriMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormThaoTac.Visible = false;
            if (!IsPostBack)
            {
                PopulateRootLevel();
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TieuDeChuyenMuc.InnerHtml = "<font color=green>Sửa thông tin Menu chuyên mục : </font><font color=red>" + TreeView1.SelectedNode.Text + "</font>";
            MenuID.Value = TreeView1.SelectedValue;
            LoadDataToControls(Convert.ToInt32(MenuID.Value));
            FormThaoTac.Visible = true;
            btnCapNhat.Text = "Cập nhật";
            btnCapNhat.CommandName = MyConst.Action.Update;
            btnXoa.Visible = true;
            btnThemSub.Visible = true;
        }

        private void LoadDataToControls(int mID)
        {
            string sql = "SELECT * FROM dbo.tblT2BMenu WHERE ID = " + mID;
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            txtTen.Text = dt.Rows[0]["Ten"].ToString();
            if (dt.Rows[0]["IsShowUp"].ToString().Equals("1"))
            {
                rbtHienThi.Checked = true;
                rbtKhongHienThi.Checked = false;
            }
            else
            {
                rbtHienThi.Checked = false;
                rbtKhongHienThi.Checked = true;
            }

        }

        private void PopulateRootLevel()
        {
            string sql = "select ID,Ten,ParentID,(select count(1) FROM tblT2BMenu WHERE ParentID=cms.ID) as childnodecount FROM tblT2BMenu as cms where ParentID IS NULL";

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            PopulateNodes(dt, TreeView1.Nodes);

        }
        private void PopulateSubLevel(int parentid, TreeNode parentNode)
        {
            string sql = "select ID,Ten,ParentID,(select count(1) FROM tblT2BMenu WHERE ParentID=cms.ID) as childnodecount FROM tblT2BMenu as cms where ParentID = " + parentid;
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            PopulateNodes(dt, parentNode.ChildNodes);
        }
        private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
        {
            foreach (DataRow dr in dt.Rows)
            {

                TreeNode tn = new TreeNode();
                string nodeid = dr["ID"].ToString();
                string text = "<b>" + dr["Ten"].ToString() + "</b>";
                int subnode = 0;
                try
                {
                    subnode = int.Parse(dr["childnodecount"].ToString().Trim());
                }
                catch { }
                if (subnode == 0)
                {
                    text = dr["Ten"].ToString();
                }
                tn.Text = text;
                tn.Value = dr["ID"].ToString();
                nodes.Add(tn);
                //If node has child nodes, then enable on-demand populating
                tn.PopulateOnDemand = ((int)dr["childnodecount"] > 0);

            }
        }

        protected void TreeView1_TreeNodePopulate1(object sender, TreeNodeEventArgs e)
        {
            PopulateSubLevel(int.Parse(e.Node.Value), e.Node);
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnCapNhat.CommandName == MyConst.Action.Addnew)
            {
                ThemMoiMenu();
            }
            else if (btnCapNhat.CommandName == MyConst.Action.Addonenew)
            {
                ThemMoiMenu(Convert.ToInt32(MenuID.Value));
            }
            else
            {
                CapNhatMenu();
            }
            TreeView1.ExpandAll();
        }

        private void ThemMoiMenu()
        {
            string sql = "INSERT INTO dbo.tblT2BMenu(Ten,ParentID,IsShowUp,CreateDate,UpdateDate)" +
                        "VALUES(@Ten,NULL,@IsShowUp,GETDATE(),GETDATE())";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@Ten", SqlDbType.NVarChar, ParameterDirection.Input, txtTen.Text));
            int HienThi = rbtHienThi.Checked == true ? 1 : 0;
            arrParams.Add(SqlHelper.CreateParameterObject("@IsShowUp", SqlDbType.Int, ParameterDirection.Input, HienThi));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
            TreeView1.Nodes.Clear();
            PopulateRootLevel();
        }

        private void ThemMoiMenu(int ParentMenuID)
        {
            string sql = "INSERT INTO dbo.tblT2BMenu(Ten,ParentID,IsShowUp,CreateDate,UpdateDate)" +
                        "VALUES(@Ten,@ParentID,@IsShowUp,GETDATE(),GETDATE())";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@Ten", SqlDbType.NVarChar, ParameterDirection.Input, txtTen.Text));
            arrParams.Add(SqlHelper.CreateParameterObject("@ParentID", SqlDbType.Int, ParameterDirection.Input, ParentMenuID));
            int HienThi = rbtHienThi.Checked == true ? 1 : 0;
            arrParams.Add(SqlHelper.CreateParameterObject("@IsShowUp", SqlDbType.Int, ParameterDirection.Input, HienThi));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
            TreeView1.Nodes.Clear();
            PopulateRootLevel();
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM dbo.tblT2BMenu WHERE ID = " + MenuID.Value;
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            if (dt.Rows[0]["ParentID"] != null)
            {
                XoaMenu();
            }
        }

        protected void XoaMenu()
        {
            string sql = "DELETE FROM dbo.tblT2BMenu WHERE ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, MenuID.Value));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
            TreeView1.Nodes.Clear();
            PopulateRootLevel();
        }

        protected void CapNhatMenu()
        {
            string sql = "UPDATE dbo.tblT2BMenu SET Ten = @Ten,IsShowUp = @IsShowUp WHERE ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@Ten", SqlDbType.NVarChar, ParameterDirection.Input, txtTen.Text));
            int HienThi = rbtHienThi.Checked == true ? 1 : 0;
            arrParams.Add(SqlHelper.CreateParameterObject("@IsShowUp", SqlDbType.Int, ParameterDirection.Input, HienThi));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, MenuID.Value));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
            TreeView1.Nodes.Clear();
            PopulateRootLevel();
        }

        protected void btnThemSub_Click(object sender, EventArgs e)
        {
            ResetField();
            btnCapNhat.CommandName = MyConst.Action.Addonenew;
            btnCapNhat.Text = "Lưu lại";
            btnXoa.Visible = false;
            btnThemSub.Visible = false;
            FormThaoTac.Visible = true;
            btnHuy.Visible = true;
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            ResetField();
            btnCapNhat.CommandName = MyConst.Action.Addnew;
            btnCapNhat.Text = "Lưu lại";
            btnXoa.Visible = false;
            btnThemSub.Visible = false;
            FormThaoTac.Visible = true;
        }

        protected void ResetField()
        {
            txtTen.Text = "";
            rbtHienThi.Checked = true;
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            FormThaoTac.Visible = false;
            ResetField();
        }
    }
}
